<?php
/**
 * Created by PhpStorm.
 * User: Wlh
 * Date: 2018/7/2
 * Time: 17:46
 */
namespace app\home\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;

class Helper extends Frontend{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = '*';
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';

    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 使用帮助主页
     */
    public function index()
    {
        $type_name = Db::table('bi_helper_classificate')->where('pid',0)->select();
        $type_children = Db::table('bi_helper_classificate')->where('pid','<>',0)->select();
        $list = $type_name;
        foreach ( $list as &$item ) {
            foreach ( $type_children as $row ) {
                if ( $row['pid'] ==  $item['id'] ) {
                    $item['children'][] = $row;
                }
            }
            if ( !isset($item['children']) ) {
                $item['children'] = '';
            }
        }
        //默认文章
        $defaultData = Db::name('helper_center')->order('id','ASC')->find();
        $this->assign('type_name',$type_name);
        $this->assign('list',$list);
        $keywords = Db::table('bi_config')->field('name,value')->where('name','keywords')->find();
        $details = Db::table('bi_config')->field('name,value')->where('name','details')->find();
        $this->assign('keywords',$keywords);
        $this->assign('details',$details);
        $this->assign('defaultData',$defaultData);
        return $this->view->fetch();
    }

    /**
     * 查询
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search(){
        $keywords = input('get.keywords');
        if($keywords){
            $res = Db::table('bi_helper_center')->where('keywords','like',"%$keywords%")->select();
            $this->assign('res',$res);
        }else{
            $res = array(
                'status' => false,
                'msg'    => '未输入关键字'
            );
            return json($res);
        }
    }

    public function getMainContent(){
        $helper_center_id = input('post.helper_center_id');
        $helper_center = Db::table('bi_helper_center')->where('classificate_id',$helper_center_id)->select();
        return json_encode($helper_center);
    }
}